﻿using UnityEngine;
using System.Collections;

public class MouseLook : MonoBehaviour
{
	public enum RotationAxes
	{
		MouseXAndY = 0,
		MouseX = 1,
		MouseY = 2
	}

	public RotationAxes axes = RotationAxes.MouseXAndY;

	public float sensitivityHor = 9.0f;
	public float sensitivityVert = 9.0f;

	public float minimumVert = -45.0f;
	public float maximumVert = 45.0f;

	private float _rotationX = 0;

	// Use this for initialization
	void Start ()
	{
		Rigidbody body = GetComponent<Rigidbody> ();
		if (body != null) {
			body.freezeRotation = true;
		}
	
	}
	
	// Update is called once per frame
	void Update ()
	{
		float rotationY = 0;
		switch (axes) {
		case RotationAxes.MouseX:
			// 左右移动: Input.GetAxis("Mouse X")
			transform.Rotate (0, Input.GetAxis ("Mouse X") * sensitivityHor, 0);
			break;
		case RotationAxes.MouseY:
			// 上下移动: Input.GetAxis("Mouse Y"), 往上是正值, 往下是负值
			_rotationX -= Input.GetAxis ("Mouse Y") * sensitivityVert;
			// 设定 _rotatoinX 允许的最大值和最小值
			_rotationX = Mathf.Clamp (_rotationX, minimumVert, maximumVert);
			rotationY = transform.localEulerAngles.y;
			transform.localEulerAngles = new Vector3 (_rotationX, rotationY, 0);
			break;
		case RotationAxes.MouseXAndY:
		default:
			_rotationX -= Input.GetAxis ("Mouse Y") * sensitivityVert;
			_rotationX = Mathf.Clamp (_rotationX, minimumVert, maximumVert);

			rotationY = Input.GetAxis ("Mouse X") * sensitivityHor;
			rotationY = transform.localEulerAngles.y + rotationY;
			transform.localEulerAngles = new Vector3(_rotationX, rotationY, 0);
			break;
		}
	
	}
}

